package com.study.samsung.Control.htControl;

import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.User;
import com.study.samsung.Service.htService.UserService;
import com.study.samsung.Utils.BaseServlet;
import com.study.samsung.Utils.Pager;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/htuser")
public class UserServlet extends BaseServlet {
    UserService us = new UserService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
//        List<User> users = us.selectAllUser();
//        request.setAttribute("users",users);
        Object obj = session.getAttribute("admin");
        if (obj==null){
            return "f:htjsp/login";
        }else {
            String inputvalue = request.getParameter("inputvalue");
            Integer pageNo;
            if (request.getParameter("pageNo")==null){
                pageNo=1;
            }else {
                pageNo=Integer.parseInt(request.getParameter("pageNo"));
            }
            Integer pageSize=2;
            Pager pager = us.rainbow(pageNo,pageSize,inputvalue);
            System.out.println(pager);
            request.setAttribute("pager",pager);
            return "f:htjsp/user";
        }

    }

    public JSONObject userfenye(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("application/json;charset=utf-8");
        String inputvalue = request.getParameter("inputvalue");
        Integer pageNo;
        if (request.getParameter("pageNo")==null){
            pageNo=1;
        }else {
            pageNo=Integer.parseInt(request.getParameter("pageNo"));
        }
        Integer pageSize=2;
        Pager pager = us.rainbow(pageNo,pageSize,inputvalue);
        System.out.println(pager);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pager",pager);
        return jsonObject;
    }

    public Integer updateStop(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer uid = Integer.parseInt(request.getParameter("id"));
        return us.updateStop(uid);
    }

    public Integer updateStart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer uid = Integer.parseInt(request.getParameter("id"));
        return us.updateStart(uid);
    }

    public Integer resetImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer uid = Integer.parseInt(request.getParameter("id"));
        Integer integer = us.resetImg(uid);
        if (integer>0){
            session.setAttribute("resetImg","已重置");
        }
        return integer;
    }
}
